package com.liang.bigdecimal;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalDemo {
    public static void main(String[] args) {

        /**
         *  BigDecimal bda1 = new BigDecimal(double);
         *  BigDecimal bda2 = new BigDecimal(double);
         *  穿入小数不精确
         */



        //创建bigdecimal对象
        BigDecimal bda1 = new BigDecimal("0.1");
        BigDecimal bda2 = new BigDecimal("0.3");

        //调用add方法相加
        BigDecimal add = bda1.add(bda2);
        System.out.println(add);

        //调用substract
        BigDecimal subtract = bda1.subtract(bda2);
        System.out.println(subtract);

        //multiply相乘
        BigDecimal multiply = bda1.multiply(bda2);
        System.out.println(multiply);

        //divide相除
       /* BigDecimal divide = bda1.divide(bda2);
        System.out.println(divide);*/

        //如果产生除不尽的情况
        //调用divide（被除数，保留位数，取舍方式）该方法已过时
        bda1.divide(bda2, 2, BigDecimal.ROUND_UP);//进一法
        bda1.divide(bda2, 2, BigDecimal.ROUND_FLOOR);//进去尾法
        bda1.divide(bda2, 2, BigDecimal.ROUND_HALF_UP);//四舍五入

        //新的方法
        bda1.divide(bda2,2,RoundingMode.UP);
        bda1.divide(bda2,2,RoundingMode.FLOOR);
        bda1.divide(bda2,2,RoundingMode.HALF_UP);

    }
}
